package web.controller;

import config.SpringContextUtil;
import entity.Classes;
import entity.Course;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import service.CourseService;
import serviceImpl.ClassesServiceImpl;
import serviceImpl.CourseServiceImpl;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "course",urlPatterns = "/course")
public class CourseServlet extends HttpServlet {
    private final String course_list_page = "/WEB-INF/views/instructor/instructor_course_list.jsp";
    private final String course_editView_page = "/WEB-INF/views/instructor/instructor_course_edit.jsp";
    private final String course_addView_page = "/WEB-INF/views/instructor/instructor_course_add.jsp";
    @Autowired
    private CourseService courseService;
    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.courseService = (CourseServiceImpl) SpringContextUtil.getBean("courseServiceImpl", CourseServiceImpl.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (action == null) {
            List<Course> courses = courseService.getAllCourses();
            req.setAttribute("courses", courses);
            RequestDispatcher dispatcher = req.getRequestDispatcher(course_list_page);
            dispatcher.forward(req, resp);
        }else if (action.equals("viewEdit")) {
            String courseID = req.getParameter("courseID");
            req.getSession().setAttribute("courseID", courseID);
            Course course = courseService.getCourseByCourseID(courseID);
            req.setAttribute("course", course);
            RequestDispatcher dispatcher = req.getRequestDispatcher(course_editView_page);
            dispatcher.forward(req, resp);
        }else if (action.equals("edit")) {
            String courseID = req.getSession().getAttribute("courseID").toString();
            String courseName = req.getParameter("courseName");
            String classID = req.getParameter("classID");
            String term = req.getParameter("term");
            Course course = new Course(courseID,classID,courseName,term);
            boolean flag = courseService.updateCourse(course);
            if (flag) {
                String message = "修改成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/course';</script>";
                resp.getWriter().write(jsCode);
            }
        }else if (action.equals("delete")) {
            String courseID = req.getParameter("courseID");
            boolean flag = courseService.deleteCourse(courseID);
            if (flag) {
                String message = "删除成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/course';</script>";
                resp.getWriter().write(jsCode);
            }
        }else if (action.equals("viewAdd")) {
            RequestDispatcher dispatcher = req.getRequestDispatcher(course_addView_page);
            dispatcher.forward(req, resp);
        }else if(action.equals("add")){
            String courseID = req.getParameter("courseID");
            String courseName = req.getParameter("courseName");
            String classID = req.getParameter("classID");
            String year = req.getParameter("year");
            String term = req.getParameter("term");
            int hour = Integer.parseInt(req.getParameter("hour"));
            Course course = new Course(courseID,classID,courseName,year,term,hour);
            boolean flag = courseService.addCourse(course);
            if (flag) {
                String message = "添加成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/course';</script>";
                resp.getWriter().write(jsCode);
            }else {
                String message = "添加失败";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');</script>";
                resp.getWriter().write(jsCode);
            }
        }else if(action.equals("search")){
            String keyword = req.getParameter("keyword");
            List<Course> courses = courseService.getCourseByCourseIDORCourseName(keyword,keyword);
            req.setAttribute("courses", courses);
            RequestDispatcher dispatcher = req.getRequestDispatcher(course_list_page);
            dispatcher.forward(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
